/***********************************************************
! @File        : FrameLessView.h
 *  @Brief       : 主窗口
 *  @Details     : 无边框,支持缩放拖拽,取消window areo效果
 *  @Author      : soda
 *  @Date        : 2023-04-28 16:53:28
 *  @Version     : v1.0
 *  @Copyright   : Copyright By soda, All Rights Reserved
 ***********************************************************/

#pragma once
#include <QMouseEvent>
#include <QQuickView>
#include <QRegion>
#include <QMap>
#include <QDir>
#include <QFileDialog>

enum SaveFileEnum{
    SaveModel = 0,
    ExportSupportScript = 1,
    ExportSupportScript_Mul = 2,
};


class FrameLessViewPrivate;
class /*TAO_API*/ FrameLessView : public QQuickView
{
    Q_OBJECT
    using Super = QQuickView;
    Q_PROPERTY(bool isMax READ isMax NOTIFY isMaxChanged)
    Q_PROPERTY(bool isFull READ isFull NOTIFY isFullChanged)
public:
    explicit FrameLessView(QWindow *parent = nullptr);
    ~FrameLessView();
    void moveToScreenCenter();
    bool isMax() const;
    bool isFull() const;
    QQuickItem *titleItem() const;
    static QRect calcCenterGeo(const QRect &screenGeo, const QSize &normalSize);


    Q_INVOKABLE QString wakeSaveFileDialog(QString title, QString defaultName, QString filters, int identify = 0){
        //QString    getSaveFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )

        QString defaultPath = QDir::homePath();

        QMap<int, QString>::Iterator iter = mSaveFilePathMap.find(identify);
        if( iter != mSaveFilePathMap.end() ){
            defaultPath = iter.value();
        }else{
            mSaveFilePathMap.insert(identify, defaultPath);
        }

        QString fileName = QFileDialog::getSaveFileName(nullptr, title, defaultPath + "/" + defaultName, filters);

        if (!fileName.isNull())
        {
            mSaveFilePathMap[identify] = fileName;

            //fileName是文件名
            return fileName;
        }
        else
        {
            //点的是取消
            return "";
        }
    }

public slots:
    void setIsMax(bool isMax);
    void setIsFull(bool isFull);
    void setTitleItem(QQuickItem* item);

    void moveCursor(int x, int y) { mCursor.setPos(x, y); }
    QPoint getCursorPosition(){ return mCursor.pos(); }
    void setViewPosition(int x, int y){ this->setPosition(x, y); }

signals:
    void isMaxChanged(bool isMax);
    void isFullChanged(bool isFull);
    void mousePressed(int xPos, int yPos, int button);
    void signalQuit();
    void signalQuitNotice();

protected:
    void showEvent(QShowEvent *e) override;
    void resizeEvent(QResizeEvent *e) override;
    bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
    void mousePressEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    bool event(QEvent *) override;

private:
    FrameLessViewPrivate *d;
    QMap<int, QString> mSaveFilePathMap;
    QCursor mCursor;
};
